<?php
/***
 * 搜索控制器
 */


namespace app\mobile\controller;
use think\Request;
use think\Db;
class Search extends MobileBase {

    public function index(){
        $type = empty(I("get.type"))?1:2;//1：窖藏 2:定制
        $where = "type = ".$type;
        if(I("get.search")){
            $where = $where." and goods_name like '%".I("get.search")."%'";
        }
        $goodslist = M("goods")->where($where)->select();
        $this->assign("goodslist",$goodslist);
        return $this->fetch();
    }
    
    public function search(){
        $conditon = I("get.keyword");
        //搜搜酒厂
        $suppliers_list = M("suppliers")->where("suppliers_name like '%$conditon%' and is_check=1")->select();
        $goods_list = M("goods")->where("goods_name like '%$conditon%' and is_on_sale=1")->order("shop_price")->select();
        //筛选搜索商品的的酒厂
        foreach ($goods_list as $key=>$value){
            $suppliers = M("suppliers")->where("suppliers_id",$value['suppliers_id'])->find();
            if(!in_array($suppliers,$suppliers_list) && $suppliers['is_check'] == 1){
                $suppliers_list[] = $suppliers;
            }
        }
        foreach ($suppliers_list as $key=>$value){
            $suppliers_list[$key]['attention'] = M('attention')->where("suppliers_id",$value['suppliers_id'])->count();
            $suppliers_list[$key]['origin_name'] = M('origin')->where('id',$value['origin_id'])->getField('title');
        }
        $this->assign("suppliers_list",$suppliers_list);
        $this->assign("goods_list",$goods_list);
        return $this->fetch();
    }
    
}